package com.debao;

import com.debao.common.core.controller.BaseController;
import com.debao.common.core.domain.AjaxResult;
import com.debao.common.core.page.TableDataInfo;
import com.debao.common.enums.OrderStatusEnum;
import com.debao.common.utils.SecurityUtils;
import com.debao.dto.CancelStadiumTableOrderDto;
import com.debao.system.domain.StadiumTableOrder;
import com.debao.system.service.IStadiumTableOrderService;
import com.debao.system.service.IStadiumTableScheduleService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 桌台订单
 */
@RestController
@RequestMapping("/api/stadiumTableOrder")
@CrossOrigin
public class ApiStadiumTableOrder extends BaseController {


    @Resource
    private IStadiumTableOrderService tableOrderService;
    @Resource
    private IStadiumTableScheduleService tableScheduleService;
    /**
     * 桌台订单
     *
     * @param stadiumTableOrder
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(StadiumTableOrder stadiumTableOrder) {
        stadiumTableOrder.setUserId(SecurityUtils.getUserId());
        startPage();
        return getDataTable(tableOrderService.selectStadiumTableOrderList(stadiumTableOrder));
    }

    /**
     * 桌台订单详情
     *
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public AjaxResult info(@PathVariable("id") Long id) {
        return success(tableOrderService.selectStadiumTableOrderById(id));
    }

    /**
     * 桌台订单详情
     *
     * @param orderNo
     * @return
     */
    @GetMapping("/infoNo/{orderNo}")
    public AjaxResult infoNo(@PathVariable("orderNo") String orderNo) {
        return success(tableOrderService.selectStadiumTableOrderByOrderNo(orderNo));
    }

    /**
     * 订单是否支付成功
     *
     * @param orderNo
     * @return
     */
    @GetMapping("/isPaySuccess/{orderNo}")
    public AjaxResult isPaySuccess(@PathVariable("orderNo") String orderNo) {
        return AjaxResult.success(tableOrderService.isPaySuccess(orderNo));
    }

    /**
     * 取消订单

     * @return
     */
    @PutMapping("/cancel")
    public AjaxResult cancelOrder(@RequestBody @Validated CancelStadiumTableOrderDto dto) {
        StadiumTableOrder order = tableOrderService.selectStadiumTableOrderByOrderNo(dto.getOrderNo());
        if (Objects.isNull(order)) {
            return error("订单不存在");
        }
        if (!order.getStatus().equals(OrderStatusEnum.None.name())) {
            return error("订单已处理");
        }
        order.setStatus(OrderStatusEnum.Cancel.name());
        int row = tableOrderService.updateStadiumTableOrder(order);
        if (row == 1) {
            // 解锁桌台
            dto.getScheduleList().forEach(schedule -> {
                // 解锁
                schedule.setUsable(1);
                schedule.setOrderNo("");
                schedule.setAppointUserId(0L);
                // 清除订单号
                tableScheduleService.updateStadiumTableSchedule(schedule);
            });
        }
        return success();

    }

}
